/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import View.InvitationRow;
import View.ReservationBrowser;
import View.ReservationTable;
import View.UserRowWrapper;
import agh.rozproszony.model.Invitation;
import agh.rozproszony.model.Reservation;
import agh.rozproszony.model.Room;
import agh.rozproszony.model.User;
import agh.rozproszony.reservations.ReservationsServer;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Marcin_PC
 */
public class ReservationController {

    ReservationsServer server;
    AdminController adminController;
    User user;

    public void addInvitations(List<UserRowWrapper> selectedUsers, String title, Reservation reservation) {
        for (UserRowWrapper user : selectedUsers) {
            if (user.getLogin() == this.user.getLogin()) {
                continue;
            }
        }
        try {//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            //Invitation invitation = new Invitation(0, server.getReservationId(reservation), server.getUserByEmail(user.getEmail()).getId(), false, title);

            for(int i=0;i<selectedUsers.size();i++)
            {
            Invitation invitation = new Invitation(0, server.getReservationId(reservation), server.getUserByEmail(selectedUsers.get(i).getEmail()).getId(), false, title);
            
            server.addInvitation(invitation);
            }
        } catch (RemoteException ex) {
            Logger.getLogger(ReservationController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ReservationController.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
    
    public List<Invitation> getAwaitingInvitations(int userId) throws RemoteException
    {
        return server.getAwaitingInvitations(userId);
    }

    public Reservation addReservation(
            String room,
            Date selectedDate,
            String from,
            String to,
            String roomId) {

        try {
            from = from.substring(0, from.indexOf(":"));
            to = to.substring(0, to.indexOf(":"));
            Reservation newReservation = new Reservation(0,
                    StrToInt(roomId),
                    user.getId(),
                    selectedDate.getYear()+1900,
                    selectedDate.getMonth()+1,
                    selectedDate.getDate(),
                    StrToInt(from),
                    StrToInt(to));

            if(server.addReservation(newReservation) == true)
            {
                 return newReservation;
            }
            return new Reservation();
        } catch (RemoteException ex) {
            Logger.getLogger(ReservationBrowser.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    /*
     * Zwraca tabele rezerwacji
     * @return ReservationTable
     */

    public ReservationTable getReservations() {
        return null;
    }

    public User getUser() {
        return user;
    }

    public ReservationController(ReservationsServer server, User user) {
        this.server = server;
        this.user = user;
        adminController = new AdminController(server);
    }

    public AdminController getAdminController() {
        return adminController;
    }
  
    public List<Room> getAllRooms() {
        try {
            return server.getAllRooms();
        } catch (RemoteException ex) {
            throw new RuntimeException("Nie mozna pobrać listy dostępnych pokoi.", ex);
        }
    }

    public List<User> getAllUsers() {
        try {
            return server.getAllUsers();
        } catch (RemoteException ex) {
            throw new RuntimeException("Nie mozna pobrać listy użytkowników.", ex);
        }
    }

    public ReservationsServer getServer() {
        return server;
    }

    /**
     *
     * @return
     */
    public List getAllReservations() throws RemoteException {
        return server.getAllReservations();
    }

    public User getUserByLogin(String login) {
        try {
            return server.getUserByLogin(login);
        } catch (RemoteException ex) {
            Logger.getLogger(ReservationController.class.getName()).log(Level.SEVERE, null, ex);
        } 
        return null;
    }

    public User getUserByEmail(String email) {
        try {
            return server.getUserByEmail(email);
        } catch (RemoteException ex) {
            Logger.getLogger(ReservationController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ReservationController.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public boolean validateTransaction(String room, String from, String to, Date selectedDate, String title) {
        try {
            if (title.equals("tytul") || title == null || title.equals("")) {
                return false;
            }

            if (room.equals("")) {
                return false;
            }
            
            if( selectedDate == null )
            {
                return false;
            }
            
            Date date = new Date();
            if (date.after(selectedDate)) {
                return false;
            }
            if (from.equals(to)) {
                return false;
            }
            DateFormat sdf = new SimpleDateFormat("hh:mm");
            Date start = sdf.parse(from);
            Date end = sdf.parse(to);

            if (start.after(end)) {
                return false;
            }

            return true;
        } catch (ParseException ex) {
            throw new RuntimeException("Validate Transaction", ex);
        }
    }

    private int StrToInt(String s) {
        Double d = new Double(s);
        return (int) d.intValue();
    }

    public List<Reservation> getAllReservation(Date from, Date to) throws RemoteException {
        return server.getAllReservations(from, to);
    }
    
    public Reservation getReservation(int id) throws RemoteException
    {
        
        return server.getReservation(id);
    }
    
   public User getUserNameById(int id) throws RemoteException{
       return server.getUserNameById(id);
       
   }

    public int deleteInvitations(InvitationRow removeUnselectedRows) throws RemoteException {
        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            return server.deleteInvitation(removeUnselectedRows);

    }
}
